<%@ taglib
uri="/WEB-INF/struts-html.tld" prefix="html" %><%@ taglib
uri="/WEB-INF/struts-bean.tld" prefix="bean" %><%@ taglib
uri="/WEB-INF/struts-logic.tld" prefix="logic" %><%@ taglib
uri="/WEB-INF/at.tld" prefix="at" %><%@ page
import="com.actimind.actitime.util.CSV,com.actimind.actitime.reports.*,
        java.util.List,
        java.util.Iterator,
        com.actimind.actitime.beans.BillingTypeBean"%>
<% response.setContentType( "text/csv" ); %>        
Report parameters<bean:define id="parameters"><jsp:include
page="billingsummaryreportparams.jsp" /></bean:define>
<at:CheckLoggedIn/><bean:define
id="report" name="report" type="BillingSummaryReport"/><bean:define
id="form" name="BillingSummaryReportForm" type="BillingSummaryReportForm"/><%

    final List columnBeans = report.getColumnBeans();
    String[] csvline = new String[ 2 + columnBeans.size() + ( form.isShowTasks() ? 1 : 0 ) + 1 ];
    int i=0;
    csvline[i++] = "Customer";
    csvline[i++] = "Project";
    if( form.isShowTasks() )
        csvline[i++] = "Task";
    csvline[i++] = "Spent Sum";
    for( Iterator iterator = columnBeans.iterator(); iterator.hasNext(); )
    {
        BillingTypeBean billingTypeBean = ( BillingTypeBean ) iterator.next();
        csvline[i++] = billingTypeBean.getName();
    }

    CSV csv = new CSV(',');
    out.println( csv.makeRow( new String[] { parameters } ) );
    out.println();
    out.println( csv.makeRow( csvline ) );

    Iterator customersIterator = report.getChildEntries().iterator();
    while( customersIterator.hasNext() )
    {
        ReportEntry customerEntry = ( ReportEntry ) customersIterator.next();
        csvline[0] = customerEntry.getName();

        Iterator projectsIterator = customerEntry.getChildEntries().iterator();
        while( projectsIterator.hasNext() )
        {
            ReportEntry projectEntry = ( ReportEntry ) projectsIterator.next();
            csvline[1] = projectEntry.getName();

            if( form.isShowTasks() )
            {
                Iterator tasksIterator = projectEntry.getChildEntries().iterator();
                while( tasksIterator.hasNext() )
                {
                    ReportEntry taskEntry = ( ReportEntry ) tasksIterator.next();
                    csvline[2] = taskEntry.getName();

                    completeCVSRow( csvline, 3, taskEntry, columnBeans );
                    out.println( csv.makeRow( csvline ) );
                }
            }
            else
            {
                completeCVSRow( csvline, 2, projectEntry, columnBeans );
                out.println( csv.makeRow( csvline ) );
            }
        }
    }

%><%!

    private void completeCVSRow( String[] csvline, int offset, ReportEntry entry, List columnBeans )
    {
        csvline[offset++] = formatWork( entry.getTotalValue() );
        for( Iterator iterator = columnBeans.iterator(); iterator.hasNext(); )
        {
            BillingTypeBean billingTypeBean = ( BillingTypeBean ) iterator.next();
            csvline[offset++] = formatWork( entry.getValue(billingTypeBean.getId()) );
        }
    }

    private String formatWork(ReportValue totalValue)
    {
        StringBuffer res = new StringBuffer();
        res.append(String.valueOf(totalValue.getValue() / 60)).append(".");
        int frac = (int)Math.round(((double)totalValue.getValue() % 60 / 60) * 100);
        if(frac < 10)
            res.append("0");
        if(frac % 10 == 0)
            frac /= 10;
        if(frac > 0)
            res.append(String.valueOf(frac));
        return res.toString();
    }

%>